package com.chenshu.shop.controller.admin;

import com.chenshu.shop.common.PageUtil;
import com.chenshu.shop.model.Order;
import com.chenshu.shop.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created with IntelliJ IDEA
 * Description
 * User：chenshu
 * Date：2024-06-07
 * Time：10:36
 */
@Controller
@RequestMapping("/admin")
@Transactional(isolation = Isolation.READ_COMMITTED, rollbackFor = Exception.class)
public class OrderController_admin {
    @Autowired
    OrderService orderService;
    @RequestMapping("orderList")
    public String getOrderList(HttpServletRequest request, Integer status,
                               @RequestParam(value = "page", defaultValue = "1", required = false) Integer pageIndex,
                               @RequestParam(value = "size", defaultValue = "3", required = false) Integer pageSize){
        List<Order> orders = orderService.getAllOrders(status, pageIndex, pageSize);
        request.setAttribute("orderList", orders);

        Integer count = orderService.getOrderCount(status);
        String pageTool = PageUtil.getPageTool(request, count, pageIndex, pageSize);
        request.setAttribute("pageTool", pageTool);
        return "forward:/admin/order_list.jsp";
    }

    @RequestMapping("/orderUpdate")
    public String updateOrderStatus(@RequestParam("id") Integer orderId, Integer status) {
        orderService.updateOrderStatus(orderId, status);
        //查找修改订单数据所在页码
        Integer page = orderService.getUpdatePage(orderId, status);
        return "redirect:/admin/orderList?status=" + status + "&page=" + page;
    }

    @RequestMapping("/orderDelete")
    public String deleteOrder(Integer id) {
        orderService.deleteOrder(id);
        return "redirect:/admin/orderList";
    }

}
